package main

import (
	"gingorm/framwork"
	"net/http"

	"github.com/gin-gonic/gin"
)

type Weather struct {
	City 	string	`json:"city",gorm:"city"`
	Low 	int32	`json:"temp_lo",gorm:"temp_lo"`
	High	int32	`json:"temp_hi",gorm:"temp_hi"`
	Prcp	float32	`json:"prcp",gorm:"prcp"`
	Date 	string	`json:"date",gorm:"date"`
}

type Cities struct {
	Name	string	`json:"name",gorm:"name"`
	Location string	`json:"location",gorm:"location"`
}

type WeatherHandle struct {
	framwork.GroupRouter
}

func (h *WeatherHandle) Process(group *gin.RouterGroup) error {
	statuCode := http.StatusOK

	group.GET("/:dbname", func(c *gin.Context) {
		DbName := c.Param("dbname")
		switch DbName {
		case "weather":
			result := make([]Weather, 10)
			dbrl := svrInstance.DB.GetDBInstance("postgresql").Table(DbName).Find(&(result))
			c.HTML(statuCode, "db_sky.html", gin.H{"title": DbName,"p1":result, "tableName":DbName, "Error":dbrl.Error})
		case "cities":
			result := make([]Cities, 10)
			dbrl := svrInstance.DB.GetDBInstance("postgresql").Table(DbName).Find(&(result))
			c.HTML(statuCode, "db_sky.html", gin.H{"title": DbName,"p1":result, "tableName":DbName, "Error":dbrl.Error})
		default:
			statuCode = http.StatusNotFound
			c.String(statuCode, "can't find database :%v", DbName)
			return
		}
	})

	return nil
}